package com.honeybee.cloud.framework.base.exception.business;

import com.honeybee.cloud.framework.base.exception.ExceptionEnum;
import lombok.Getter;

/**
 * @Description 业务基础异常
 * @Author DELL
 * @Create 2018-12-08
 * @Since 1.0.0
 */
public class BaseBizException extends RuntimeException {

    /**
     * 异常父级编码
     */
    @Getter
    protected Integer parentCode;

    /**
     * 异常子级编码
     */
    @Getter
    protected Integer subCode;

    /**
     * 异常原因
     */
    @Getter
    protected String msg;

    /**
     * 异常子级原因
     */
    @Getter
    protected String subMessage;

    /**
     * 异常数据
     */
    @Getter
    protected Object data;

    public BaseBizException(Integer parentCode, Integer subCode, String msgFormat, Object... args) {
        super(String.format(msgFormat, parentCode, subCode, args));
        this.parentCode = parentCode;
        this.subCode = subCode;
        this.msg = String.format(msgFormat, args);
        this.subMessage = String.format(msgFormat, args);
    }

    public BaseBizException(Integer parentCode, Integer subCode) {
        super(String.format("", parentCode, subCode));
        this.parentCode = parentCode;
        this.subCode = subCode;
    }

    public BaseBizException(Integer parentCode, Integer subCode, String message, String subMessage) {
        super(String.format(message, parentCode, subCode));
        this.parentCode = parentCode;
        this.subCode = subCode;
        this.msg = message;
        this.subMessage = subMessage;
    }

    public BaseBizException(Integer parentCode, Integer subCode, String message, String subMessage, Object data) {
        super(String.format(message, parentCode, subCode));
        this.parentCode = parentCode;
        this.subCode = subCode;
        this.msg = message;
        this.subMessage = subMessage;
        this.data = data;
    }

    public BaseBizException(Integer parentCode, String msgFormat, Object... args) {
        super(String.format(msgFormat, parentCode, null, args));
        this.parentCode = parentCode;
        this.msg = String.format(msgFormat, args);
    }

    public BaseBizException() {
        super();
    }


    public BaseBizException(String message, Throwable cause) {
        super(message, cause);
        this.msg = message;
    }

    public BaseBizException(Throwable cause) {
        super(cause);
    }

    public BaseBizException(String message) {
        super(message);
        this.msg = message;
    }

    public static BaseBizException getInstance(BaseBizExceptionEnum exceptionEnum) {
        return new BaseBizException(exceptionEnum.getParentCode(), exceptionEnum.getSubCode(), exceptionEnum.getParentMessage(), exceptionEnum.getMessage());
    }

    public static BaseBizException getInstance(ExceptionEnum exceptionEnum) {
        return new BaseBizException(exceptionEnum.getCode(),  null, exceptionEnum.getRemark(), (String) null);
    }

    public static BaseBizException getInstance(BaseBizExceptionEnum exceptionEnum, Object data) {
        return new BaseBizException(exceptionEnum.getParentCode(), exceptionEnum.getSubCode(), exceptionEnum.getParentMessage(), data);
    }
}
